package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.dto.ShoppingOrderVo;
import com.ddx.dto.ShortVideoDto;
import com.ddx.service.IShoppingOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 订单页面
 */
@RestController
@RequestMapping(value = "/app/order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    IShoppingOrderService iShoppingOrderService;

    /**
     * 我的订单列表页面，all全部，0 待付款 1待发货 2待收货 3 待评价 4售后 5已售罄 6订单取消
     * @param request
     * @param shoppingOrderVo
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public ResultModel list(HttpServletRequest request, @RequestBody ShoppingOrderVo shoppingOrderVo) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            resultModel.setData(iShoppingOrderService.list(userId, shoppingOrderVo.getStatus().toString()));
        } catch (Exception e) {
            logger.error("我的订单列表页面，0 待付款 1待发货 2待收货 3 待评价 4售后 5已售罄 6订单取消：异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    /**
     * 订单详情，传订单id
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/detail", method = {RequestMethod.POST})
    public ResultModel detail(HttpServletRequest request, @RequestBody ShoppingOrderVo shoppingOrderVo) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            resultModel.setData(iShoppingOrderService.orderInfo(shoppingOrderVo.getOrderId()));
        } catch (Exception e) {
            logger.error("订单详情，传订单id页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    /**
     * 查看物流状态
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/wuLiu", method = { RequestMethod.POST })
    public ResultModel wuLiu(HttpServletRequest request,@RequestBody ShoppingOrderVo shoppingOrderVo) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            resultModel.setData(iShoppingOrderService.orderStatus(shoppingOrderVo.getOrderId()));
        } catch (Exception e) {
            logger.error("订单详情，传订单id页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 查看物流状态
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/editWuLiu", method = { RequestMethod.POST })
    public ResultModel editWuLiu(HttpServletRequest request,@RequestBody ShoppingOrderVo shoppingOrderVo) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            iShoppingOrderService.editWuLiu(shoppingOrderVo);
        } catch (Exception e) {
            logger.error("订单详情，传订单id页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    /**
     * 下单
     * @param request
     * @param map
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/submitOrder")
    public ResultModel submitOrder(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            String userId = request.getHeader("userId");
            String productId = map.get("productId").toString();
            String userAddressId = map.get("userAddressId").toString();
            Integer productNumber = Integer.parseInt(map.get("productNumber").toString());
            resultModel = iShoppingOrderService.submitOrder(userId, productId, productNumber, userAddressId);
        } catch (Exception e) {
            logger.error("下单失败，请稍后重试", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
